<?php

namespace App\Modules\Admin\Http\Controllers;

use App\Traits\Json;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\DB;

class BaseController extends Controller
{
    use Json;

    protected $service;
    /**
     * 构造函数
     * @author 牧羊人
     * @since 2020/11/10
     * BaseController constructor.
     */
    public function __construct()
    {
        // 初始化网络请求配置
        $this->initRequestConfig();

        // 初始化系统常量
        $this->initSystemConst();
    }
    /**
     * 列表页
     *
     * @param  \Illuminate\Http\Request  $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        if (!isset($this->service)){
            return $this->successJson([], '请先设置Service或者重写方法！');
        }
        return $this->successJson($this->service->lists($request->all()));
    }

    /**
     * 详情
     *
     * @param  int  $id
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail(Request $request)
    {
        if (!isset($this->service)){
            return $this->successJson([], '请先设置Service或者重写方法！');
        }
        if ($detail = $this->service->detail($request)){
            return $this->successJson($detail);
        }else{
            return $this->errorJson('数据不存在！');
        }
    }

    /**
     * 新增流程
     *
     * @param $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function createService($request)
    {
        if ($request instanceof FormRequest){
            $request->validated();
        }

        if (!isset($this->service)){
            return $this->successJson([], '请先设置Service或者重写方法！');
        }
        if ($this->service->create($request->all())){
            return $this->successJson([], $this->service->getError());
        }else{
            return $this->errorJson($this->service->getError());
        }
    }

    /**
     * 更新流程
     *
     * @param $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateService($request)
    {
        if ($request instanceof FormRequest){
            $request->validated();
        }
        if (!isset($this->service)){
            return $this->successJson([], '请先设置Service或者重写方法！');
        }
        if ($this->service->update($request->all())){
            return $this->successJson([], $this->service->getError());
        }else{
            return $this->errorJson($this->service->getError());
        }
    }

    /**
     * 删除
     *
     * @param  \Illuminate\Http\Request  $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request)
    {
        if (!isset($this->service)){
            return $this->successJson([], '请先设置Service或者重写方法！');
        }
        if ($this->service->delete($request->all())){
            return $this->successJson([], $this->service->getError());
        }else{
            return $this->errorJson($this->service->getError());
        }
    }

    /**
     * 指定字段变更操作
     *
     * @param  \Illuminate\Http\Request  $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function changeFiledStatus(Request $request)
    {
        if (!isset($this->service)){
            return $this->successJson([], '请先设置Service或者重写方法！');
        }

        if ($this->service->changeFiledStatus($request->all())){
            return $this->successJson([], $this->service->getError());
        }else{
            return $this->errorJson($this->service->getError());
        }
    }

    /**
     * 下拉筛选列表（可搜索）
     *
     * @param  \Illuminate\Http\Request  $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function getSelectLists(Request $request)
    {
        $lists = $this->service->getSelectLists($request);
        return $this->successJson($lists);
    }

    public function initRequestConfig(){

    }
    public function initSystemConst(){
        // 项目根目录
        defined('ROOT_PATH') or define('ROOT_PATH', base_path());

        // 文件上传目录
        defined('ATTACHMENT_PATH') or define('ATTACHMENT_PATH', base_path('public/uploads'));

        // 图片上传目录
        defined('IMG_PATH') or define('IMG_PATH', base_path('public/uploads/images'));

        // 临时存放目录
        defined('UPLOAD_TEMP_PATH') or define('UPLOAD_TEMP_PATH', ATTACHMENT_PATH . "/temp");

        // 定义普通图片域名
        defined('IMG_URL') or define('IMG_URL', env('IMG_URL'));

        // 系统全称
        define('SITE_NAME', env('SITE_NAME'));
        // 系统简称
        define('NICK_NAME', env('NICK_NAME'));
        // 系统版本号
        define('VERSION', env('VERSION'));
    }
}
